package com.hong.utils.forkjoin;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveTask;
import java.util.stream.IntStream;

/**
 * @Auther: Administrator
 * @Date: 2018/7/25 12:25
 * @Description:拆分算法
 */
public class ForkJoinRecursiveTask {

    public static void main(String[] args) {
        final ForkJoinPool forkJoinPool = new ForkJoinPool();
        ForkJoinTask<Integer> futrue = forkJoinPool.submit(new CalculatedRecursiveTask(0, 1000));
        try {
            Integer result = futrue.get();
            System.out.println(result);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

    private static class CalculatedRecursiveTask extends RecursiveTask<Integer>{

        private final static int MAX_THRESHOLD = 200;

        private final int start;

        private final int end;

        private CalculatedRecursiveTask(int start, int end) {
            this.start = start;
            this.end = end;
        }


        @Override
        protected Integer compute() {

            if(end-start<=MAX_THRESHOLD){
                return IntStream.rangeClosed(start,end).sum();
            }else{
                int middle = (start+end)/2;
                CalculatedRecursiveTask leftTask  = new CalculatedRecursiveTask(start,middle);
                CalculatedRecursiveTask rightTask  = new CalculatedRecursiveTask(middle + 1,end);
                leftTask.fork();
                rightTask.fork();

                return leftTask.join()+rightTask.join();

            }
        }
    }
}
